package com.jbp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jbp.dao.SkillParamDao;
import com.jbp.model.SkillParam;
import com.jbp.model.SkillParamGroup;
import com.jbp.model.SkillUserParam;
import com.jbp.service.SkillParamGroupService;
import com.jbp.service.SkillParamService;
import com.jbp.service.SkillUserParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

/**
 * 技能参数(SkillParam)表服务实现类
 * @author cc
 * @since 2020-11-19 17:06:55
 */
@Service
public class SkillParamServiceImpl extends ServiceImpl<SkillParamDao, SkillParam> implements SkillParamService {
    @Autowired
    private SkillUserParamService skillUserParamService;

    @Autowired
    private SkillParamGroupService skillParamGroupService;

    //申请参数列表
    @Override
    public List<SkillParam> applyList(Integer skillId) {
        QueryWrapper<SkillParam> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("skill_id", skillId);
        queryWrapper.eq("status", 1);
        queryWrapper.in("scene", 1, 2);
        queryWrapper.orderByAsc("sort");
        return list(queryWrapper);
    }

    //修改参数列表
    @Override
    public List<SkillParam> editList(Integer skillId, Integer userId) {
        QueryWrapper<SkillParam> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("skill_id", skillId);
        queryWrapper.eq("status", 1);
        queryWrapper.in("scene", 1, 3);
        queryWrapper.orderByAsc("sort");
        List<SkillParam> list = list(queryWrapper);
        for (SkillParam skillParam : list) {
            SkillUserParam skillUserParam = skillUserParamService.getOne(new QueryWrapper<SkillUserParam>()
                    .eq("user_id", userId)
                    .eq("skill_id", skillId)
                    .eq("param_id", skillParam.getId())
            );
            if (skillUserParam!=null) {
                skillParam.setValue(skillUserParam.getParamValue());
            }
        }
        return list;
    }

    /**
     * 获取分组后的参数列表及参数的值
     * @param skillId 技能id
     * @param userId 大神id
     * @return
     */
    @Override
    public List<SkillParamGroup> editListV2(Integer skillId, Integer userId) {
        QueryWrapper<SkillParam> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("skill_id", skillId);
        queryWrapper.eq("status", 1);
        queryWrapper.in("scene", 1, 3);
        queryWrapper.orderByAsc("sort");
        List<SkillParam> list = list(queryWrapper);
        // 保存参数组的id列表
        List<Integer> paramGroupIds = new ArrayList<>();
        // 保存参数组id和该参数组包含的参数列表
        Map<Integer, List<SkillParam>> groupedSkillParams = new HashMap<>();
        // 保存group_id为空的skillParam
        List<SkillParam> noGroupIdSkillParams = new ArrayList<>();

        for (SkillParam skillParam : list) {
            if (skillParam.getGroupId() == null) {
                noGroupIdSkillParams.add(skillParam);
            } else {
                // 暂存用户技能参数组的id列表
                paramGroupIds.add(skillParam.getGroupId());
                // 一个组中的SkillParam列表
                if (groupedSkillParams.containsKey(skillParam.getGroupId())) {
                    List<SkillParam> oneGroupedSkillParams = groupedSkillParams.get(skillParam.getGroupId());
                    oneGroupedSkillParams.add(skillParam);
                } else {
                    List<SkillParam> oneGroupedSkillParams = new ArrayList<>();
                    oneGroupedSkillParams.add(skillParam);
                    groupedSkillParams.put(skillParam.getGroupId(), oneGroupedSkillParams);
                }
            }

            SkillUserParam skillUserParam = skillUserParamService.getOne(new QueryWrapper<SkillUserParam>()
                    .eq("user_id", userId)
                    .eq("skill_id", skillId)
                    .eq("param_id", skillParam.getId())
            );
            if (skillUserParam!=null) {
                skillParam.setValue(skillUserParam.getParamValue());
            }
        }

        // 查询该用户技能参数的组列表，并按照参数组的order排序
        QueryWrapper<SkillParamGroup> groupQueryWrapper = new QueryWrapper<>();
        groupQueryWrapper.in("id", paramGroupIds);
        groupQueryWrapper.orderByAsc("sort");
        List<SkillParamGroup> skillParamGroups = skillParamGroupService.list(groupQueryWrapper);

        // 保存存在的groupIds。用paramGroupIds集合减去existedGroupIds，就是不存在的组id。
        // 对于param配置了不存在的组id，和group_id为null一样处理
        List<Integer> existedGroupIds = new ArrayList<>();

        Comparator<SkillParam> compareBySort = (SkillParam o1, SkillParam o2) -> o1.getSort().compareTo( o2.getSort() );

        // 构造按照顺序排列的参数组
        for(SkillParamGroup skillParamGroup: skillParamGroups) {
            existedGroupIds.add(skillParamGroup.getId());
            List<SkillParam> oneGroupedSkillParams = groupedSkillParams.get(skillParamGroup.getId());
            // 组内排序
            Collections.sort(oneGroupedSkillParams, compareBySort);
            skillParamGroup.setSkillParams(oneGroupedSkillParams);
        }

        List<Integer> nonExistedGroupIds = new ArrayList<>();
        for(Integer paramGroupId: paramGroupIds) {
            if (!existedGroupIds.contains(paramGroupId)) {
                nonExistedGroupIds.add(paramGroupId);
            }
        }

        // 对于group_id为空，或者group_id不存在的SkillParam，放到默认组中
        SkillParamGroup defaultSkillParamGroup = null;
        if (noGroupIdSkillParams.size() != 0 || nonExistedGroupIds.size() != 0){
            defaultSkillParamGroup = new SkillParamGroup();
            defaultSkillParamGroup.setId(0);
            defaultSkillParamGroup.setName("其他");
            defaultSkillParamGroup.setSort(Integer.MAX_VALUE);
            List<SkillParam> defaultGroupIdSkillParams = new ArrayList<>(noGroupIdSkillParams);
            for (Integer nonExistedGroupId : nonExistedGroupIds) {
                defaultGroupIdSkillParams.addAll(groupedSkillParams.get(nonExistedGroupId));
            }
            // 给defaultGroupIdSkillParams按照sort排序
            Collections.sort(defaultGroupIdSkillParams, compareBySort);

            defaultSkillParamGroup.setSkillParams(defaultGroupIdSkillParams);
        }

        // 对于group_id为null的skillParam，放到默认组中
        if (defaultSkillParamGroup != null) {
            skillParamGroups.add(defaultSkillParamGroup);
        }

        return skillParamGroups;
    }

    @Override
    public List<SkillParam> filterParams(Integer skillId) {
        QueryWrapper<SkillParam> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("skill_id", skillId);
        queryWrapper.eq("status", 1);
        queryWrapper.in("scene", 1, 2, 3);
        queryWrapper.in("type",  2, 3, 8);
        queryWrapper.orderByAsc("sort");
        return list(queryWrapper);
    }
}